<?php

namespace App\Http\Controllers;

use App\Models\CommentModel;
use Illuminate\Http\Request;
use App\Models\BlogModel;
use Illuminate\Support\Facades\Auth;

class deleteController extends Controller
{
    //删除文章
    public function delete(Request $request){
        $res = BlogModel:: where('blog_id',$request->id)->delete();
//        dd($res);
        if ($res!==1){
            echo '<script>alert("删除失败");self.location=document.referrer;</script>';
        }else{
            CommentModel:: where('blog_id',$request->id)->delete();
            echo '<script>self.location=document.referrer;</script>';
        }
    }

        //发表文章
        public function add(Request $request){
            $data = [
                'title'=>$request->title,
                'content'=>$request->contentt,
                'user_id'=>$request->user_id,
                'type'=>$request->type
            ];
            if(BlogModel::insert($data)){
                echo '<script>alert(\'发表成功\');self.location=document.referrer;</script>';
            }else{
                echo '<script>alert(\'发表失败\');self.location=document.referrer;</script>';
            }

        }

    //编辑文章
    public function update(Request $request){
        $arr = [
            'title'=>$request->title,
            'content'=>$request->contentt,
            'type'=>$request->type
        ];
        $id = $request->blog_id;
        BlogModel::where('blog_id',$id)->update($arr);
        echo '<script>alert(\'修改成功\');self.location=document.referrer;</script>';
    }

}
